﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class AddProducts : Form
    {
        ProductDao dao;
        UnitDao unidao;
        private int id;
        public AddProducts()
        {
            InitializeComponent();
            dao = new ProductDao();
            unidao = new UnitDao();
            loadCbx();
        }

        public AddProducts(int id)
        {
            this.id = id;
            InitializeComponent();
            dao = new ProductDao();
            unidao = new UnitDao();
            loadCbx();
            loadTaiSan(id);
        }

        private void loadTaiSan(int id)
        {
            DataRow row = dao.getProDuctById(id);
            txtName.Text = row["NAME"].ToString();
            txtNotes.Text = row["NOTES"].ToString();
            txtPrice.Text = row["PRICE"].ToString();
            txtProducer.Text = row["PRODUCER"].ToString();
            cbxunit.SelectedValue = int.Parse(row["UNIT_ID"].ToString());
        }

        private void loadCbx()
        {
            DataTable table = unidao.getUnits("");
            if (table != null)
            {
                cbxunit.DataSource = table;
                cbxunit.DisplayMember = "NAME";
                cbxunit.ValueMember = "ID";
            }

        }
        private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
            && !char.IsDigit(e.KeyChar)
            && e.KeyChar != '.')
            {
                e.Handled = true;
            }
            if (e.KeyChar == '.'
                && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            if (txtName.Text.Trim() == "" || txtPrice.Text.Trim() == "")
            {
                Error er = new Error(UserConstant.PRODUCT_INPUT);
                er.ShowDialog();
                er.Dispose();
            }
            else
            {
                float total = 0;
                if (float.TryParse(txtPrice.Text.Trim(), out total))
                {
                    if (this.id <= 0)
                    {
                        if (dao.insertProduct(txtName.Text.Trim(), total, txtNotes.Text.Trim(), LoginDao.userID, int.Parse(cbxunit.SelectedValue.ToString()), "F", txtProducer.Text.Trim()))
                        {
                            Result re = new Result(UserConstant.SAVE_SUCCESSFUL);
                            re.ShowDialog();
                            re.Dispose();
                            txtName.Text = "";
                            txtNotes.Text = "";
                            txtPrice.Text = "";
                            txtProducer.Text = "";
                        }
                        else
                        {
                            Error er = new Error(UserConstant.SAVE_FAIL);
                            er.ShowDialog();
                            er.Dispose();
                        }
                    }
                    else
                    {
                        if (dao.updateProduct(this.id, txtName.Text.Trim(), txtProducer.Text.Trim(), total, txtNotes.Text.Trim(), "F", int.Parse(cbxunit.SelectedValue.ToString())))
                        {
                            Result re = new Result(UserConstant.SAVE_SUCCESSFUL);
                            re.ShowDialog();
                            re.Dispose();
                            this.Close();
                        }
                        else
                        {
                            Error er = new Error(UserConstant.SAVE_FAIL);
                            er.ShowDialog();
                            er.Dispose();
                        }
                    }
                }
                else
                {
                    Error er = new Error(UserConstant.PRODUCT_INPUT);
                    er.ShowDialog();
                    er.Dispose();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtNotes.Text = "";
            txtPrice.Text = "";
            txtProducer.Text = "";
        }

        private String RemoveComma(String value)
        {
            if (value != null && value.Contains(","))
            {
                value = value.Replace(",", String.Empty).Trim();
            }
            return value;
        }
        private void txtPrice_TextChanged(object sender, EventArgs e)
        {
            if (RemoveComma(txtPrice.Text).Length > 3)
            {
                txtPrice.Text = decimal.Parse(RemoveComma(txtPrice.Text)).ToString("#,###");
                txtPrice.Select(txtPrice.Text.Length, 0);
            }
        }
    }
}
